﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;

namespace BLL.Settings
{
    public class Setting: BaseSetting
    {
        #region "Constructors"
        public Setting() { }

        public Setting(int id, string name, string value)
        {
            this.ID = id;
            this.Name = name;
            this.Name = name;
        }
        #endregion

        #region "Public Properties"

        public string Name { get; set; }
        
        public string Value { get; set; }

        #endregion

        #region Get Setting From SettingsDetail
        private static Setting GetSettingFromSettingsDetail(SettingsDetails record)
        {
            if (record == null)
                return null;
            else
                return new Setting(record.Id, record.Name, record.Value);
        }

        private static List<Setting> GetSettingFromListSettingsDetail(List<SettingsDetails> recordset)
        {
            List<Setting> settings = new List<Setting>();
            foreach (SettingsDetails record in recordset)
                settings.Add(GetSettingFromSettingsDetail(record));
            return settings;
        }
        #endregion

        #region Method

        public static Setting GetSetting(int id)
        {
            Setting setting = null;
            string key = "Setting_" + id.ToString();
            if (BaseSetting.Settings.EnableCaching && BizObject.Cache[key] != null)
                setting = (Setting)BizObject.Cache[key];
            else
            {
                setting = GetSettingFromSettingsDetail(SiteProvider.Setting.SettingGet(id));
                BaseSetting.CacheData(key, setting);
            }
            return setting;
        }

        public static string GetSettingValue(string name)
        {
            string setting = null;
            string key = "SettingValue_" + name;
            if (BaseSetting.Settings.EnableCaching && BizObject.Cache[key] != null)
                setting = (string)BizObject.Cache[key];
            else
            {
                setting = SiteProvider.Setting.SettingGetValue(name.Trim());
                BaseSetting.CacheData(key, setting);
            }
            return setting;
        }

        public static List<Setting> ListSettings()
        {
            List<Setting> setting = null;
            string key = "Setting";
            if (BaseSetting.Settings.EnableCaching && BizObject.Cache[key] != null)
                setting = (List<Setting>)BizObject.Cache[key];
            else
            {
                List<SettingsDetails> recordset = SiteProvider.Setting.ListSetting();
                setting = GetSettingFromListSettingsDetail(recordset);
                BaseSetting.CacheData(key, setting);
            }
            return setting;
        }

        public static bool UpdateSetting(string name, string value)
        {
            value = Helpers.ConvertNullToEmptyString(value);
            SettingsDetails record = new SettingsDetails(0, name, value);
            bool ret = SiteProvider.Setting.SettingUpdate(record);
            BizObject.PurgeCacheItems("Setting");
            return ret;
        }

        #endregion
    }
}
